﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;
using System;
using System.Threading;
using WindowsInput;

namespace DevelopTool.Forms.TextProcessForms
{
    /// <summary>
    /// 辅助输入
    /// </summary>
    public partial class AuxiliaryInputForm : BaseUIPage
    {

        public AuxiliaryInputForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 开始输入
        /// </summary>
        public void StartInput()
        {
            if(this.backgroundWorker.IsBusy)
            {
                this.ShowWarningNotifier("已经开始输入了，别急~");
                return;
            }
            else
            {
                this.Start();
            }
        }


        private void AuxiliaryInputForm_Load(object sender,EventArgs e)
        {
            this.Disposed += this.AuxiliaryInputForm_Disposed;

            this.btnStart.Text = $"开始输入({HotKeyHelper.GetWinfromHotKeyIntoShow(Setting.AuxiliaryInput.StartHotKey)})";
        }




        /// <summary>
        /// 开始输入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender,EventArgs e)
        {
            this.Start();
        }

        /// <summary>
        /// 开始输入
        /// </summary>
        private void Start()
        {
            if(this.upDownStartSecond.Value <= 0)
            {
                this.ShowWarningNotifier("开始时间不能小于1秒");
                return;
            }

            if(this.upDownDelayMillisecond.Value < 10)
            {
                this.ShowWarningNotifier("延迟输入时间不能小于10毫秒");
                return;
            }

            if(string.IsNullOrEmpty(this.codeEditorControl.ContentText))
            {
                this.ShowWarningNotifier("请输入文本");
                return;
            }

            if(this.backgroundWorker.IsBusy)
            {
                this.ShowWarningNotifier("已经开始输入了，别急啊~");
                return;
            }


            this.btnStart.Enabled = false;

            int countdown = this.upDownStartSecond.Value;

            AuxiliaryInputTips.SetTipsText(" " + countdown.ToString());

            AuxiliaryInputTips.IsCancel = false;

            //倒计时
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer
            {
                Interval = 1000,
                Enabled = true//启动计时器
            };
            timer.Tick += delegate (object ss,EventArgs ee)
            {
                if(AuxiliaryInputTips.IsCancel)
                {
                    timer.Stop();
                    return;
                }

                if(countdown <= 1)
                {
                    timer.Stop();

                    //开始后台任务 
                    backgroundWorker.RunWorkerAsync(new Tuple<string,int>(this.codeEditorControl.ContentText,this.upDownDelayMillisecond.Value));
                }
                else
                {
                    countdown--;//倒计时
                    AuxiliaryInputTips.SetTipsText(" " + countdown.ToString());
                }
            };
        }


        private void backgroundWorker_DoWork(object sender,System.ComponentModel.DoWorkEventArgs e)
        {
            Tuple<string,int> tuple = e.Argument as Tuple<string,int>;

            InputSimulator inputSimulator = new InputSimulator();

            foreach(char c in tuple.Item1)
            {
                if(AuxiliaryInputTips.IsCancel || this.backgroundWorker.CancellationPending) break;//取消输入

                inputSimulator.Keyboard.TextEntry(c);

                backgroundWorker.ReportProgress(0,c);//报告状态，展示结果

                Thread.Sleep(tuple.Item2);//休眠
            }
        }


        private void backgroundWorker_ProgressChanged(object sender,System.ComponentModel.ProgressChangedEventArgs e)
        {
            AuxiliaryInputTips.SetTipsText(" " + e.UserState.ToString());
        }


        private void backgroundWorker_RunWorkerCompleted(object sender,System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if(AuxiliaryInputTips.IsCancel)
            {
                this.ShowInfoNotifier("取消输入");
            }
            else
            {
                this.ShowSuccessNotifier("输入完成");
            }

            AuxiliaryInputTips.CloseTips();
            this.btnStart.Enabled = true;
        }


        private void AuxiliaryInputForm_Disposed(object sender,EventArgs e)
        {
            try
            {
                this.backgroundWorker.CancelAsync();
                this.backgroundWorker.Dispose();
            }
            catch(Exception ex)
            {
                LogHelper.WriteErrLog(ex);
            }
        }
    }
}
